var express = require('express');
var BackstageCommodity = express.Router();
var dbcomm = require('./util/dbcomm');
var moment = require('moment');
var bodyparser = require('body-parser');
var now = moment().format('YYYY年MM月DD日') ;//日期

BackstageCommodity.use(bodyparser.urlencoded({extended:false}));


//添加商品信息
BackstageCommodity.get('/addCommodity',function(req,res){
    var dates = req.query;
    console.log(dates);
    var sql = "INSERT INTO `ourbarterterrace`.`sys_commodity` (commodityName,commodityThumbnail, commodityPrise,commodityTarget,commodityComplete,commodityDescribe,commodityState,userId)"+
    " VALUES ('"+dates.commodityName+"', '"+dates.commodityThumbnail+"', '"+dates.commodityPrise+"','"+dates.commodityTarget+"','"+dates.commodityComplete+"','"+dates.commodityDescribe+"',"+0+",'"+dates.userId+"')";
    console.log(sql);
    dbcomm.query(sql,function(err,data){
        if(err)throw err;
        else{
            if(data.affectedRows == 1){
                console.log("添加成功");
                res.redirect('/Backstage/frame/groundingcommodity.html');//跳转到商品上架//////////////////////////////////////////
            }else{
                console.log("添加失败");
                res.redirect('/Backstage/frame/addcommodity.html');
            }
            
        }
    });
});


//修改商品信息
BackstageCommodity.post('/updatecommodity',function(req,res){
    var dates = req.body;
    console.log(dates);
    var sql = "UPDATE `ourbarterterrace`.`sys_commodity` SET `commodityName`='"+dates.commodityName+"', `commodityThumbnail`='"+dates.commodityThumbnail+"',"+
    " `commodityPrise`="+dates.commodityPrise+", `commodityTarget`='"+dates.commodityTarget+"', `commodityComplete`='"+dates.commodityComplete+"',"+
    "`commodityDescribe`='"+dates.commodityDescribe+"' WHERE  `commodityId`="+dates.commodityId+";";
    console.log(sql);
    dbcomm.query(sql,function(err,data){
        if(err)throw err;
        else{
            console.log(data);
            if(data.affectedRows == 1){
                res.send(data);
            }else{
                res.send(data);
            }
            
        }
    });
});


//展示商品
BackstageCommodity.get('/showCommodity',function(req,res){
    var query = req.query;
    console.log(query);
    var star = parseInt(query.page - 1) * parseInt(query.rows);//query.page是当前页数,query.rows是每一页显示多少行数据
    var end = parseInt(query.rows);
    var sql = "select * from sys_commodity where commodityState = 1 limit ?,?";
    var myjson = {};
    dbcomm.query(sql,[star,end],function (err,data){
    //    if (err) throw err;
        // console.log(data);
        myjson.rows = data;//当前是多少行
        dbcomm.query("select * from sys_commodity where commodityState = 1 ",function (err,data){
            if (err) throw err;
            console.log(data);
            myjson.total = data.length;//总共有多少行
            res.send(myjson);
        });
    });
});

//展示未上架商品
BackstageCommodity.get('/showNewCommodity',function(req,res){
    var query = req.query;
    var star = parseInt(query.page - 1) * parseInt(query.rows);//query.page是当前页数,query.rows是每一页显示多少行数据
    var end = parseInt(query.rows);
    var sql = "select * from sys_commodity where commodityState = 0 or commodityState = 3 limit ?,?";
    var myjson = {};
    dbcomm.query(sql,[star,end],function (err,data){
        myjson.rows = data;//当前是多少行
        dbcomm.query("select * from sys_commodity where commodityState = 0 or commodityState = 3 ",function (err,data){
            if (err) throw err;
            // console.log(data);
            myjson.total = data.length;//总共有多少行
            res.send(myjson);
        });
    });
});


//上架商品
BackstageCommodity.get('/adoptCommodity',function(req,res){
    var dates = req.query;
    console.log(dates);
    var sql = "UPDATE `ourbarterterrace`.`sys_commodity` SET `commodityState`= 1 WHERE  `commodityId`="+dates.commodityId+";";
    console.log(sql);
    dbcomm.query(sql,function(err,data){
        if(err)throw err;
        else{
            console.log(data);
            if(data.affectedRows == 1){
                console.log("审核通过");
            }else{
                console.log("审核失败，请重试");
            }
            
        }
    });
});

//下架商品
BackstageCommodity.get('/frozeCommodity',function(req,res){
    var dates = req.query;
    console.log(dates);
    var sql = "UPDATE `ourbarterterrace`.`sys_commodity` SET `commodityState`= 3 WHERE  `commodityId`="+dates.commodityId+";";
    console.log(sql);
    dbcomm.query(sql,function(err,data){
        if(err)throw err;
        else{
            console.log(data);
            if(data.affectedRows == 1){
                console.log("下架成功");
            }else{
                console.log("下架失败");
            }
            
        }
    });
});



//删除商品
BackstageCommodity.get('/deleteCommodity',function(req,res){
    var dates = req.query;
    console.log(dates);
    var sql = "delete from sys_commodity WHERE  `commodityId`="+dates.commodityId+";";
    console.log(sql);
    dbcomm.query(sql,function(err,data){
        if(err)throw err;
        else{
            console.log(data);
            if(data.affectedRows == 1){
                console.log("删除成功");
            }else{
                console.log("删除失败,请重试");
            }
            
        }
    });
});

//查找用户
BackstageCommodity.get('/finduser',function(req,res){
    var dates = req.query;
    console.log("fsa");
    // console.log(dates);
    var sql = "select userName from sys_user where userId = "+dates.userId+"";
    dbcomm.query(sql,function (err,data){
            if (err) {throw err;}
            else{
                if(data.length ==1){
                    data[data.length]={'result' : 'ok'};
                    console.log(data);
                    // console.log(data[0].userName);
                    res.send(data);
                }
                else{
                    data[data.length]={'result' : 'sorry'};
                    console.log(data);
                    res.send(data); 
                }
            }
    });
});



//修改用户密码
BackstageCommodity.get('/remUserPass',function(req,res){
    var dates = req.query;
    console.log(dates);
    var sql = "update sys_user set userPass ='"+dates.userPass+"' where userId = "+dates.userId+"";
    console.log(sql);
    dbcomm.query(sql,function (err,data){
            if (err) {throw err;}
            else{
                console.log(data);
                // res.json(data[0]);
                res.send(data);
            }
    });
});


module.exports = BackstageCommodity;